package com.mlethe.library.calendarview;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

import androidx.annotation.NonNull;

/**
 * 月视图普通控件,可自由继承实现
 *
 * @Author Mlethe
 * @Date 2022/12/29
 */
public abstract class MonthView extends BaseMonthView {

    public MonthView(Context context) {
        super(context);
    }

    /**
     * 开始绘制
     *
     * @param canvas   canvas
     * @param calendar 对应日历
     * @param row      行
     * @param column   列
     * @param position 位置
     */
    protected void draw(@NonNull Canvas canvas, @NonNull Calendar calendar, int row, int column, int position) {
        int x = column * mItemWidth + mDelegate.getCalendarPaddingLeft();
        int y = row * mItemHeight;
        onLoopStart(x, y);
        boolean isSelected = position == mCurrentItem;
        boolean hasScheme = calendar.hasScheme();

        if (hasScheme) {
            //标记的日子
            boolean isDrawSelected = false;//是否继续绘制选中的onDrawScheme
            if (isSelected) {
                isDrawSelected = onDrawSelected(canvas, calendar, x, y, true);
            }
            if (isDrawSelected || !isSelected) {
                //将画笔设置为标记颜色
                mSchemePaint.setColor(calendar.getSchemeColor() != 0 ? calendar.getSchemeColor() : mDelegate.getSchemeThemeColor());
                onDrawScheme(canvas, calendar, x, y);
            }
        } else {
            if (isSelected) {
                onDrawSelected(canvas, calendar, x, y, false);
            }
        }
        onDrawText(canvas, calendar, x, y, hasScheme, isSelected);
    }

    @Override
    public void onClick(View view) {
        if (!isClick) return;
        int position = getIndex();
        if (position == -1) return;
        Calendar calendar = mItems.get(position);
        boolean isCurrentMonth = calendar.isCurrentMonth();
        if (mDelegate.getMonthViewShowMode() == CalendarViewDelegate.MODE_ONLY_CURRENT_MONTH && !isCurrentMonth) {
            return;
        }
        if (!isInRange(calendar)) {
            if (mDelegate.mCalendarSelectListener != null) {
                mDelegate.mCalendarSelectListener.onCalendarOutOfRange(calendar);
            }
            return;
        }
        mDelegate.setSelectedCalendar(calendar);
        if (isCurrentMonth) {
            mCurrentItem = position;
            invalidate();
        } else {
            mCurrentItem = -1;
            if (mOnCalendarClickListener != null) {
                mOnCalendarClickListener.onClick(calendar, position);
            }
        }
        if (mDelegate.mCalendarSelectListener != null) {
            mDelegate.mCalendarSelectListener.onCalendarSelect(calendar, isClick);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mDelegate.mCalendarLongClickListener == null) return false;
        if (!isClick) return false;
        int position = getIndex();
        if (position == -1) return false;
        Calendar calendar = mItems.get(position);
        boolean isCurrentMonth = calendar.isCurrentMonth();
        if (mDelegate.getMonthViewShowMode() == CalendarViewDelegate.MODE_ONLY_CURRENT_MONTH && !isCurrentMonth) {
            return false;
        }
        if (!isInRange(calendar)) {
            if (mDelegate.mCalendarLongClickListener != null) {
                mDelegate.mCalendarLongClickListener.onCalendarLongClickOutOfRange(calendar);
            }
            return true;
        }
        mDelegate.setSelectedCalendar(calendar);
        if (isCurrentMonth) {
            mCurrentItem = position;
            invalidate();
        } else {
            mCurrentItem = -1;
            if (mOnCalendarClickListener != null) {
                mOnCalendarClickListener.onClick(calendar, position);
            }
        }
        if (mDelegate.mCalendarSelectListener != null) {
            mDelegate.mCalendarSelectListener.onCalendarSelect(calendar, isClick);
        }
        if (mDelegate.mCalendarLongClickListener != null) {
            mDelegate.mCalendarLongClickListener.onLongClick(calendar);
        }
        return true;
    }

    /**
     * 绘制选中的日期
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return 是否绘制onDrawScheme，true or false
     */
    protected abstract boolean onDrawSelected(@NonNull Canvas canvas, @NonNull Calendar calendar, int x, int y, boolean hasScheme);

    /**
     * 绘制标记的日期,这里可以是背景色，标记色什么的
     *
     * @param canvas   canvas
     * @param calendar 日历calendar
     * @param x        日历Card x起点坐标
     * @param y        日历Card y起点坐标
     */
    protected abstract void onDrawScheme(@NonNull Canvas canvas, @NonNull Calendar calendar, int x, int y);

    /**
     * 绘制日历文本
     *
     * @param canvas     canvas
     * @param calendar   日历calendar
     * @param x          日历Card x起点坐标
     * @param y          日历Card y起点坐标
     * @param hasScheme  是否是标记的日期
     * @param isSelected 是否选中
     */
    protected abstract void onDrawText(@NonNull Canvas canvas, @NonNull Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected);
}
